<template>
  <div class="app-container">
    <el-button type="primary" class="btn" size="mini" @click="handleAdd" v-hasPermi="['pet:rec:add']">新增活动</el-button>
    <el-table v-loading="loading" :data="recList">
      <el-table-column label="活动名称" align="center" prop="name" />
      <el-table-column label="有效期开始日期" align="center" prop="beginDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.beginDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期截至日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="充值金额" align="center" prop="recPrice" />
      <el-table-column label="赠送金额" align="center" prop="givePrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pet:rec:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pet:rec:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改充值活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" class="center" width="500px" append-to-body>
      <el-form ref="form" :model="form" class="input" :rules="rules" label-width="100px">
        <el-form-item label="活动名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入活动名称" />
        </el-form-item>
        <el-form-item label="有效期开始" prop="beginDate">
          <el-date-picker clearable v-model="form.beginDate" style="width: 100%;" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择有效期开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="有效期截至" prop="endDate">
          <el-date-picker clearable v-model="form.endDate" style="width: 100%;" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择有效期截至日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="充值金额" prop="recPrice">
          <el-input v-model="form.recPrice" class="input-radius-right" placeholder="请输入充值金额">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="赠送金额" prop="givePrice">
          <el-input v-model="form.givePrice" class="input-radius-right" placeholder="请输入赠送金额">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="btn" style="padding: 12px 70px;" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRec, getRec, delRec, addRec, updateRec } from "@/api/pet/rec";

export default {
  name: "Rec",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 充值表格数据
      recList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        name: null,
        beginDate: null,
        endDate: null,
        recPrice: null,
        givePrice: null,
        updateUser: null,
        createUser: null,
      },
      // 表单参数
      echargeForm: {},
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询充值列表 */
    getList() {
      this.loading = true;
      listRec(this.queryParams).then(response => {
        this.recList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        name: null,
        beginDate: null,
        endDate: null,
        recPrice: null,
        givePrice: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加充值";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRec(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改充值";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRec(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRec(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除充值编号为"' + ids + '"的数据项？').then(function () {
        return delRec(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pet/rec/export', {
        ...this.queryParams
      }, `rec_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
